<?php

namespace App\Http\Model;

use App\Lib\PvLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
 * @desc 格式化json 数据
 * Class OutJson
 * @package app\api\service
 */

class OutJson
{
    /**
     * @desc 返回带数据的json string
     * @param $code
     * @param array $data
     * @param string $format
     */
    static function outJsonResult($code, $data = array(), $format = 'data')
    {
        $res['code'] = $code;
        if ($code == -1){
            header('HTTP/1.1 500 Internal Server Error');
        }
        $res['msg'] = isset(Hint::$Msg[$code]) ? Hint::$Msg[$code] : '失败';
        
        $format = empty($format) ? 'data' : $format;

        $res[$format] = $data;

        if($code != 0 && empty($data)){
            unset($res[$format]);
        }
        
        $callback = empty($_GET['callback']) ? '' : trim($_GET['callback']);
        $jsonStr = json_encode($res,JSON_UNESCAPED_UNICODE);
        if (empty($callback)) {
            echo $jsonStr;
        } else {
            echo "{$callback}(" . $jsonStr . ")";
        }
        //self::_pvLog($res,$callback); by wj
        exit;
    }
    
    /**
     * @desc 返回没有数据的json string
     * @param $code
     * @param array $data
     * @param string $format
     */
    static function outJsonNoData($code, $data = array(), $format = 'data')
    {
        
        $res['code'] = $code;
        if ($code == -1){
            header('HTTP/1.1 500 Internal Server Error');
        }
        $res['msg'] = isset(Hint::$Msg[$code]) ? Hint::$Msg[$code] : '失败';
        
        $format = empty($format) ? 'data' : $format;
        
        if ($data) {
            $res[$format] = $data;
        } else {

        }
        
        $callback = empty($_GET['callback']) ? '' : trim($_GET['callback']);
        $jsonStr = json_encode($res,JSON_UNESCAPED_UNICODE);
        if (empty($callback)) {
            echo $jsonStr;
        } else {
            echo "{$callback}(" . $jsonStr . ")";
        }
        //self::_pvLog($res,$callback); by wj
        exit;
    }

    /**
     * @desc 返回没有数据的json string
     * @param $code
     * @param array $data
     * @param string $format
     * @author zxiaofee
     */
    static function outJsonNoDataNoCallback($code, $data = array(), $format = 'data')
    {

        $res['code'] = $code;
        if ($code == -1){
            header('HTTP/1.1 500 Internal Server Error');
        }
        $res['msg'] = isset(Hint::$Msg[$code]) ? Hint::$Msg[$code] : '失败';

        $format = empty($format) ? 'data' : $format;

        if ($data) {
            $res[$format] = $data;
        } else {

        }

        $jsonStr = json_encode($res,JSON_UNESCAPED_UNICODE);
        echo $jsonStr;
        //self::_pvLog($res, ''); by wj
        exit;
    }
    
    
    static function outJsonCustom($code, $msg = '', $format = 'data')
    {
        
        $res['code'] = $code;
        if ($code == -1){
            header('HTTP/1.1 500 Internal Server Error');
        }

        if($msg !== '') {
            $res['msg'] = $msg;
        } else {
            $res['msg'] = isset(Hint::$Msg[$code]) ? Hint::$Msg[$code] : '失败';
        }
        
        $format = empty($format) ? 'data' : $format;
        
        
        $callback = empty($_GET['callback']) ? '' : trim($_GET['callback']);
        $jsonStr = json_encode($res,JSON_UNESCAPED_UNICODE);
        if (empty($callback)) {
            echo $jsonStr;
        } else {
            echo "{$callback}(" . $jsonStr . ")";
        }
        //self::_pvLog($res,$callback); by wj
        exit;
    }

    static function _pvLog($response, $callback){
        $user = new User();
        PvLog::$response = $response;
        PvLog::$requestTime = date('Y-m-d H:i:s');
        PvLog::$ip = client_ip();
        PvLog::$userId =  $user->get_current_user_id();
        PvLog::$cookie = $user->get_current_user_cookie();
        PvLog::write();
    }
}
